package com.terry.bootdemo.web.controller;

import com.terry.bootdemo.web.common.JsonUtil;
import com.terry.bootdemo.web.entity.UserEntity;
import com.terry.bootdemo.web.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class LoginController {
    private final Logger logger = Logger.getLogger(getClass());
    @Autowired
    private UserService userService;

    @RequestMapping("/echo")
    @ResponseBody
    public String echo() {
        return "Hi 涛2!";
    }

    @RequestMapping("/login.do")
    public ModelAndView login(String usercode, String password, HttpServletRequest request, RedirectAttributes redirect) {
        System.out.println("进入登录哈哈哈哈");
        try {
            UserEntity userEntity = userService.Login(usercode, password);
            Date date = userEntity.getCreateTime();
            System.out.println("时间=" + date);
            //HttpSession session= request.getSession();
            // session.setAttribute("name",userEntity.getName());
            request.setAttribute("name", userEntity.getName());
            // redirect.addFlashAttribute("name",userEntity.getName());
            System.out.println(userEntity.getName());
        } catch (Exception e) {
            System.out.println(e);
            //request.setAttribute("erromessage",e.getMessage());
            redirect.addFlashAttribute("erromessage", e.getMessage());
            return new ModelAndView("redirect:/templates");
        }
        // response.sendRedirect("/index.do");

        return new ModelAndView("index");
    }

    //处理用户名唯一性的判断
    @RequestMapping("/userJudge.action")
    @ResponseBody
    public String userJudge(String phone_mobile) {
        System.out.println("哈哈哈" + phone_mobile);
        JsonUtil jsonUtil = new JsonUtil();
        UserEntity data = null;
        try {
            data = userService.FindEntityByPhone(phone_mobile);
        } catch (Exception e) {
            logger.info(e);
        }
        System.out.println(jsonUtil.obj2Json(data) + "呵呵呵");
        return jsonUtil.obj2Json(data);
    }

    @RequestMapping("/goregister.do")
    public String goregister() {
        return "register";
    }

    @RequestMapping("/register.do")
    public String register(HttpServletRequest request) {
        String phone_mobile = request.getParameter("phone_mobile");
        String account = request.getParameter("account");
        String login_password = request.getParameter("login_password");
        try {
            //获取当前注册时间
            Date date = new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String crentetime = df.format(date);
            Date ct = df.parse(crentetime);
            JsonUtil jsonUtil = new JsonUtil();
            UserEntity userEntity = new UserEntity();
            userEntity.setCreateTime(ct);
            userEntity.setPassword(login_password);
            userEntity.setPhone(phone_mobile);
            userEntity.setUserName(account);
            System.out.println("呼呼" + jsonUtil.obj2Json(userEntity));
            userService.AddUser(userEntity);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return "register";
        }
        return "login";
       /* Map<String,Object> map = new HashMap<String, Object>();
        map.put("phone_mobile", userEntity.getPhone());
        map.put("login_password", userEntity.getPassword());

        //判断页面传回的数据要求
        Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[01236789]))\\d{8}$");
        Matcher matcher = pattern.matcher(userEntity.getPhone());
        if(userEntity.getPhone()==null || userEntity.getPhone()==null || !matcher.matches()){
            return "index.jsp";
        }

        //获取当前注册时间
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("register_time", df.format(date));

        //生成唯一识别码
        String s = UUID.randomUUID().toString();
        String user_code =  s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
        map.put("user_code", user_code);

        //将数据添加到数据库中

        return "pages/register_success.jsp";*/
    }

    @RequestMapping("/index.do")
    public String index() {
        return "index";
    }

    @RequestMapping("/templates")
    public String hello(HttpServletRequest request) {
        request.setAttribute("hello", "涛");
        System.out.println("hahahahah");
        return "login";
    }

}
